<?php

namespace app\api\controller\shop;

use app\admin\model\Staffdevice;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Staff extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function add()
    {
        $mobile = $this->request->param('mobile');
        $name = $this->request->param('name');


        $user = $this->auth->getUser();
        $userModel = new \app\admin\model\User();
        $staffUser = $userModel->where('mobile',$mobile)->find();
        if(!$staffUser){
            $db = array();
            $db['group_id'] = 1;
            $db['username'] = $name;
            $db['mobile'] = $mobile;
            $db['createtime'] = time();
            $db['status'] = 'normal';

            $id = $userModel->insertGetId($db);
        }else{
            if($staffUser['group_id'] == 2) $this->error("此手机号已是代理商身份不可添加");
            $id = $staffUser['id'];
        }
        $db = array();
        $db['mobile'] = $mobile;
        $db['name'] = $name;
        $db['agent_id'] = $user['id'];
        $db['user_id'] = $id;

        $staffModel = new \app\admin\model\Staff();
        $staffModel->insert($db);
        $this->success('成功',$id);
    }


    public function staffList()
    {
        $user = $this->auth->getUser();
        $staffModel = new \app\admin\model\Staff();
        $staffDeviceModel = new Staffdevice();
        $list = $staffModel->where('agent_id',$user['id'])->select();
        foreach ($list as $key=>$val){
            $count = $staffDeviceModel->where('user_id',$val['user_id'])->count('id');
            $list[$key]['device'] = $count;
        }
        $this->success('',$list);
    }

    public function getUnDeviceList()
    {
        $keyword = $this->request->param('keyword');
        $where = array();
        if($keyword != ""){
            $where['device_name'] = ['like', '%' . trim($keyword) . '%'];
        }
        $user = $this->auth->getUser();
        $deviceModel = new \app\admin\model\Device();
        $list = $deviceModel->where($where)->where('agent_id',$user['id'])->select();
        $staffDeviceModel = new Staffdevice();
        $bindList= $staffDeviceModel->where('agent_id',$user['id'])->select();
        $result = array();
        foreach ($list as $key=>$val){
            $val['isChecked'] = false;
            $isOk = -1;
            foreach ($bindList as $k=>$v){
                if($val['device_id'] == $v['device_id']){
                    $isOk = 1;
                }
            }
            if($isOk == -1){
                $result[] = $val;
            }
        }
        $this->success('',$result);
    }

    public function bindDevice()
    {

        $user = $this->auth->getUser();
        $user_id = $this->request->param('user_id');
        $deviceIds = $this->request->param('deviceIds');
        $items = json_decode(htmlspecialchars_decode($deviceIds), true);
        $staffDeviceModel = new Staffdevice();
        foreach ($items as $key=>$val){
            $db = array();
            $db['user_id']= $user_id;
            $db['device_id']= $val;
            $db['agent_id']= $user['id'];
            $staffDeviceModel->insert($db);
        }
        $this->success('成功');
    }

    public function bindedDeviceList()
    {
        $user_id = $this->request->param('user_id');
        $keyword = $this->request->param('keyword');
        $where = array();
        if($keyword != ""){
            $where['Device.device_name'] = ['like', '%' . trim($keyword) . '%'];
        }
        $staffDeviceModel = new Staffdevice();
        $list = $staffDeviceModel->with(['device'])->where($where)->where('user_id',$user_id)->select();
        $this->success('',$list);
    }

    public function removeBindDevice()
    {
        $user_id = $this->request->param('user_id');
        $deviceIds = $this->request->param('deviceIds');
        $items = json_decode(htmlspecialchars_decode($deviceIds), true);
        $staffDeviceModel = new Staffdevice();
        foreach ($items as $key=>$val){
            $staffDeviceModel->where('device_id',$val)->where('user_id',$user_id)->delete();
        }
        $this->success('成功');
    }

    public function deleteStaff()
    {
        $user = $this->auth->getUser();
        $user_id = $this->request->param('user_id');
        $staffModel = new \app\admin\model\Staff();
        $staffDeviceModel = new Staffdevice();
        $staff = $staffModel->where('user_id',$user_id)->where('agent_id',$user['id'])->find();
        if($staff){

            $staffModel->where('id',$staff['id'])->delete();
            $staffDeviceModel->where('user_id',$user_id)->delete();
            $this->success('成功');
        }else{
            $this->error("无权操作");
        }
    }
}
